package com.oracle.controller;

import com.oracle.annotation.Action;
import com.oracle.annotation.ActionType;
import com.oracle.config.service.api.SystemChartApi;
import com.oracle.config.service.api.SystemConfigApi;
import com.oracle.pojo.Sysconfig;
import com.oracle.vo.PageVo;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/sysconfig")
public class SysconfigController {

    @Autowired(required = false)
    private SystemConfigApi systemConfigApi;
    @Autowired(required = false)
    private SystemChartApi systemChartApi;

    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/addview")
    public String addViewSysconfig(){
        return "admin/sysconfig/addview";
    }

    @Action(ActionType.ADD)
    @RequestMapping("/add")
    public String addSysconfig(Sysconfig sysconfig){
        ResultEntity resultEntity = this.systemConfigApi.add(sysconfig);
        return "admin/comm/success";
    }

    @Action(ActionType.QUERY)
    @RequestMapping("/list")
    public String sysconfigList(@RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer pageIndex,@RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize, Model model){
        ResultEntity<PageVo<Sysconfig>> resultEntity = this.systemConfigApi.queryPage(pageIndex, pageSize);
        model.addAttribute("pv",resultEntity.getData());
        return "admin/sysconfig/list";
    }


    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/editview")
    public String editView(Integer id,Model model){
        ResultEntity<Sysconfig> resultEntity = this.systemConfigApi.getSysconfigById(id);
        model.addAttribute("config",resultEntity.getData());
        return "admin/sysconfig/editview";
    }

    @Action(ActionType.UPDATE)
    @RequestMapping("/edit")
    public String editSysconfig(Sysconfig sysconfig){
        ResultEntity resultEntity = this.systemConfigApi.update(sysconfig);
        return "admin/comm/success";
    }

    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/logschartview")
    public String logsChartView(){
        return "admin/sysconfig/logschart";
    }

    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/newschartview")
    public String newsChartView(){
        return "admin/sysconfig/newschart";
    }

    @Action(ActionType.QUERY)
    @RequestMapping("/logschart")
    @ResponseBody
    public List<Map<String,Object>> logsChart(){
        ResultEntity<List<Map<String,Object>>> resultEntity = this.systemChartApi.getExecuteTypeAndCount();
        List<Map<String,Object>> mapList = resultEntity.getData();
        return mapList;
    }

    @Action(ActionType.QUERY)
    @RequestMapping("/newschart")
    @ResponseBody
    public List<Map<String,Object>> newsChart(){
        ResultEntity<List<Map<String,Object>>> resultEntity = this.systemChartApi.getNewsCategoryAndCount();
        List<Map<String,Object>> mapList = resultEntity.getData();
        return mapList;
    }



}
